<!DOCTYPE html>
<title>Subframe loading from Web Bundles</title>
<link
  rel="help"
  href="https://github.com/WICG/webpackage/blob/master/explainers/subresource-loading.md"
/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>

const frame_url = 'urn:uuid:429fcc4e-0696-4bad-b099-ee9175f023ae';

urn_uuid_iframe_test(
  'location.href',
  frame_url,
  'location.href in urn uuid iframe.');

urn_uuid_iframe_test(
  '(' + (() => {
    try {
      let result = window.localStorage;
      return 'no error';
    } catch (e) {
      return e.name;
    }
  }).toString() + ')()',
  'SecurityError',
  'Accesing window.localStorage should throw a SecurityError.');

urn_uuid_iframe_test(
  '(' + (() => {
    try {
      let result = window.sessionStorage;
      return 'no error';
    } catch (e) {
      return e.name;
    }
  }).toString() + ')()',
  'SecurityError',
  'Accesing window.sessionStorage should throw a SecurityError.');

urn_uuid_iframe_test(
  '(' + (() => {
    try {
      let result = document.cookie;
      return 'no error';
    } catch (e) {
      return e.name;
    }
  }).toString() + ')()',
  'SecurityError',
  'Accesing document.cookie should throw a SecurityError.');

urn_uuid_iframe_test(
  '(' + (() => {
    try {
      let request = window.indexedDB.open("db");
      return 'no error';
    } catch (e) {
      return e.name;
    }
  }).toString() + ')()',
  'SecurityError',
  'Opening an indexedDB should throw a SecurityError.');

urn_uuid_iframe_test(
  'window.caches === undefined',
  true,
  'window.caches should be undefined.');

function urn_uuid_iframe_test(code, expected, name) {
  promise_test(async () => {
    const link = document.createElement('link');
    link.rel = 'webbundle';
    link.href = '../resources/wbn/urn-uuid.wbn';
    link.resources = frame_url;
    document.body.appendChild(link);
    const iframe = document.createElement('iframe');
    iframe.src = frame_url;
    const load_promise = new Promise((resolve) => {
      iframe.addEventListener('load', resolve);
    });
    document.body.appendChild(iframe);
    await load_promise;
    assert_equals(
        await evalInIframe(iframe, code),
        expected);
    document.body.removeChild(link);
    document.body.removeChild(iframe);
  }, name);
}

async function evalInIframe(iframe, code) {
  const message_promise = new Promise((resolve) => {
      const listener = (e) => {
        window.removeEventListener('message', listener);
        resolve(e.data);
      }
      window.addEventListener('message', listener);
    });
  iframe.contentWindow.postMessage(code,'*');
  return message_promise;
}
</script>
</body>
